#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    cfg=new Config(CFG_FILENAME);
    db=new DB(DB_FILENAME,cfg,this);
    s=new Stock(cfg,NO_FLAG,this);
    Global::setGlobalStock(s);                  //set the global stock instance
    statusLabel=new QLabel(this);
    d=new DepotHandler(s,cfg,this);

    //qDebug()<<s;

    d->attachDepotNameListView(ui->depotListView);
    d->attachStockNameListView(ui->stockListView);
    d->attachStockTableView(ui->depotStockView);
    d->attachStandardLabels(ui->liquidityLabel,ui->standardProvisionLabel,ui->currentLiquidityLabel);
    d->attachTradeTable(ui->tradesTableView);
    d->show();

    //dialogs:
    stockDialog=new NewStockDialog(s,this);
    depotDialog=new NewDepotDialog(d,this);

    /*The connection status label in the status bar*/
    ui->statusBar->insertPermanentWidget(0,statusLabel);

    s->show(ui->stockView);   //show the tableview

    /*connect signals*/
    connect(ConnectionHandler::instance()->getQHttp(),SIGNAL(stateChanged(int)),this,SLOT(connectionChanged(int)));

    /*menu actions*/
    connect(ui->actionChart,SIGNAL(triggered()),this,SLOT(on_actionChart_triggered()));
    connect(ui->actionNewStock,SIGNAL(triggered()),stockDialog,SLOT(show()));
    connect(ui->actionNewDepot,SIGNAL(triggered()),depotDialog,SLOT(show()));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete statusLabel;
    delete cfg;
    delete stockDialog;
    delete d;
    delete s;
    delete db;
}

void MainWindow::connectionChanged(int state)
{
    QString tmp;
    qDebug()<<"Status: "<<state;
    switch(state)
    {
    case 2:
    case 3:
        statusLabel->setText("Verbinde");break;
    case 4:
        statusLabel->setText("Verbunden");break;
    case 6:
    case 0:
    default:
        statusLabel->setText("Getrennt");
    }
}

void MainWindow::on_actionChart_triggered()
{
    //chart->show();
}

/*on double click on a cell -> pop up more information (chart,...)*/
void MainWindow::viewDoubleClicked(QModelIndex index)
{

}
